package com.jh.jingfei.mine.ui;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jh.jingfei.R;
import com.jh.jingfei.utils.CacheUtils;
import com.jh.jingfei.widget.MyToolbar;

import java.io.File;

import me.yokeyword.fragmentation_swipeback.SwipeBackFragment;

/**
 * @author lxb
 * 我的设置
 * A simple {@link Fragment} subclass.
 * Use the {@link SettingFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class SettingFragment extends SwipeBackFragment implements View.OnClickListener{

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private MyToolbar setting_top_toolbar;

    private TextView cache_size_txt;
    private String mParam1;
    private String mParam2;

    public SettingFragment() {
        // Required empty public constructor
    }

    public static SettingFragment newInstance(String param1, String param2) {
        SettingFragment fragment = new SettingFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_setting, container, false);
        return attachToSwipeBack(v);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setting_top_toolbar = view.findViewById(R.id.setting_top_toolbar);
        setting_top_toolbar.setTitle("设置");
        setting_top_toolbar.setRightMenuGone(true);
        cache_size_txt = view.findViewById(R.id.cache_size_txt);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        new Thread(new Runnable() {
            @Override
            public void run() {
                File cacheDir = getContext().getCacheDir();
                try {
                    final String c = CacheUtils.getCacheSize(cacheDir);

                    Log.i("Cache", c);

                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            cache_size_txt.setText(c);
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    @Override
    public void onClick(View v) {

    }
}
